package simpleSpring.mybatis;

import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;

import java.util.Set;

/**
 * @author caihe
 * @date 2022/10/26 13:43
 */
public class CaiheScanner extends ClassPathBeanDefinitionScanner {
    
    public CaiheScanner(BeanDefinitionRegistry registry) {
        super(registry);
    }
    

    @Override
    protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
        return beanDefinition.getMetadata().isInterface();
    }

    @Override
    protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
        // 利用父类在doScan中使用重写的isCandidateComponent方法来决定获取那些BeanDefinition
        Set<BeanDefinitionHolder> beanDefinitionHolders = super.doScan(basePackages);
        for (BeanDefinitionHolder beanDefinitionHolder : beanDefinitionHolders) {
            BeanDefinition beanDefinition = beanDefinitionHolder.getBeanDefinition();
            // 保持扫描出来的名字 xxxDao
            beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(beanDefinition.getBeanClassName());
            // 设置本来的名字
            beanDefinition.setBeanClassName(CaiheFactoryBean.class.getName());
        }
        
        return super.doScan(basePackages);
    }
}
